#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<list>
#include<algorithm>

//取一个网址的协议 域名 资源名
void Geturl()
{
	//string url = "https://legacy.cplusplus.com/reference/string/string/c_str/";
	string url="https://gitee.com/cyqqwe/following";
	size_t pos1 = url.find("://");
	//协议
	string protocol;
	if (pos1 != string::npos)
	{
		protocol = url.substr(0, pos1);
	}
	cout << protocol << endl;

	size_t pos2 = url.find("/", pos1 + 3);
	//域名
	string domain;
	//资源名
	string uri;
	if (pos2 != string::npos)
	{
		domain = url.substr(pos1 + 3, pos2 - (pos1 + 3));
		uri = url.substr(pos2 + 1);
	}
	cout << domain << endl;
	cout << uri << endl;


}

int main()
{
	string s1;
	string s2 = "hello world";
	string s3(10, 'x');
	string s4("hello world", 5);
	string s5(s2, 6);
	string s6(s2);

	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << s5 << endl;
	cout << s6 << endl;

	//尾插单个字符
	s2.push_back('x');
	cout << s2 << endl;

	//追加字符串
	s4.append(" world");
	cout << s4 << endl;

	//string& operator+=
	s6 += " hello world";
	cout << s6 << endl;
	s6 += s1;
	cout << s6 << endl;

	//char& operator[] (size_t pos);
	//const char& operator[] (size_t pos) const;
	//遍历
	for (int i = 0; i < s2.size(); i++)
	{
		cout << s2[i] << " ";   //s1.operater[i]
	}
	cout << endl;

	//string& size() 不包含'\0'  编译器不会显示打印'\0'
	cout << s2.size() << endl;

	for (int i = 0; i < s2.size(); i++)
	{
		cout << ++s2[i] << " ";   //s1.operater[i]
	}
	cout << endl;

	//迭代器
	string::iterator it = s2.begin();
	while (it != s2.end())
	{
		//写
		(*it)++;
		it++;
	}

	it = s2.begin();
	while (it != s2.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	//范围for的底层是迭代器
	for (auto& ch : s2)
	{
		ch++;
	}
	for (auto& ch : s2)
	{
		cout << ch << " ";
	}
	cout << endl;

	//任何容器都支持迭代器，并且用法类似  iterator停供一种统一的方式用来访问和修改容器数据
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(2);
	v.push_back(4);
	
	vector<int>::iterator vit = v.begin();
	while (vit != v.end())
	{
		cout << *vit << " ";
		vit++;
	}
	cout << endl;

	list<int> lt;
	lt.push_back(10);
	lt.push_back(30);
	lt.push_back(20);
	lt.push_back(40);

	list<int>::iterator lit = lt.begin();
	while (lit != lt.end())
	{
		cout << *lit << " ";
		lit++;
	}
	cout << endl;

	//迭代器和算法结合  算法通过迭代器处理容器中的数据
	reverse(v.begin(), v.end());
	reverse(lt.begin(), lt.end());

	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;
	for (auto& e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	sort(v.begin(), v.end());
	for (auto& e : v)
	{
		cout << e << " ";
	}
	cout << endl;

	//反向迭代器
	string s7("hello world");
	//string::reverse_iterator rit = s7.rbegin();
	auto rit = s7.rbegin();
	while (rit != s7.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;

	//const_iterator    const_reverse_iterator  只能读不能写

	cout << s7.size() << endl;
	cout << s7.length() << endl;

	//不同的编译器有不同的接口 运行结果不一样
	cout << s7.max_size() << endl;
	cout << s7.capacity() << endl;

	//扩容机制
	int old = s7.capacity();
	for (int i = 0; i < 100; i++)
	{
		s7 += 'x';
		if (old != s7.capacity())
		{
			cout << "扩容：" << s7.capacity() << endl;
		}
		old = s7.capacity();
	}

	//clear会清理size但不会清理capacity
	cout << s7.size() << endl;
	cout << s7.capacity() << endl;
	s7.clear();
	cout << s7.size() << endl;
	cout << s7.capacity() << endl;

	/*string s8;
	getline(cin, s8);
	cout<<s8<<endl;*/

	//扩容
	string s9;
	s9.reserve(100);
	int cap = s9.capacity();
	cout << "now capacity:" << s9.capacity() << endl;

	for (int i = 0; i < 100; i++)
	{
		s9 += 'x';
		if (cap != s9.capacity())
		{
			cout << "make capacity:" << s9.capacity() << endl;
			cap = s9.capacity();
		}
	}

	//缩容  缩不缩，缩多少取决于编译器、底层、是否有数据
	s9.clear();
	s9.reserve(10);
	cout << "capacity:" << s9.capacity() << endl;

	//扩容+填值
	string s10("hello world");
	s10.reserve(100);
	cout << s10.size() << endl;
	cout << s10.capacity() << endl;

	s10.resize(200,'x');
	cout << s10.size() << endl;
	cout << s10.capacity() << endl;

	//删除数据 不会缩容
	s10.resize(20);
	cout << s10.size() << endl;
	cout << s10.capacity() << endl;

	//operator[] 和at
	try {
		string s11("hello world");
		s11.at(15);  //抛出异常
		//s11[15];  //暴力检查 断言
	}
	catch (const exception& e)
	{
		cout << e.what() << endl;
	}

	//字符串覆盖
	s10.assign("aaaaaaaa");
	cout << s10 << endl;

	//插入字符串
	s10.insert(0, "hello");
	s10.insert(5, 1, 'b');
	s10.insert(s10.end(), 10, 'y');
	cout << s10 << endl;

	//删除字符
	string s11("hello world");
	s11.erase(5, 1);
	cout << s11 << endl;
	s11.erase(5);
	cout << s11 << endl;
	s11.erase(s11.begin());
	cout << s11 << endl;

	//字符串替换
	string s12("hello world");
	s12.replace(6, 5, "xxxxxx");
	cout << s12 << endl;

	//把空格替换成20%
	string temp;
	for (auto ch : s12)
	{
		if (ch == ' ')
		{
			temp += "20%";
		}
		else
		{
			temp += ch;
		}
	}
	s12 = temp;
	cout << s12 << endl;

	//与c的一些接口函数配合 const char* c_str() const
	/*string filename = "test.cpp";
	FILE* fout = fopen(filename.c_str(), "r");*/

	Geturl();
	



	return 0;
}